//
//  YHVisitorView.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/6/25.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit
import SnapKit

class YHVisitorView: UIView {
    
    //按钮点击的闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        
        //添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messsageLabel)
        addSubview(loginBtn)
        addSubview(registerButton)
        
        //添加约束
        // 圆圈
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        // 遮罩
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        // 房子
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        //文字
        messsageLabel.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSizeMake(224, 36))
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
        }
        //登录
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(messsageLabel)
            make.top.equalTo(messsageLabel.snp_bottom).offset(16)
        }
        //注册
        registerButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(messsageLabel)
            make.top.equalTo(loginBtn)
        }
        
    }
    
    
    //懒加载控件
    //圆圈
    private lazy var feedImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    //遮罩
    private lazy var maskImageView: UIImageView = UIImageView(imgName:  "visitordiscover_feed_mask_smallicon")
    //房子
    private lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    //文字
    private lazy var messsageLabel:UILabel = {
        
        let lab = UILabel()
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(14)
        lab.textAlignment = .Center
        return lab
    }()
    //登录
    lazy var loginBtn:UIButton = {
    
        let button = UIButton()
        button.addTarget(self, action: "buttonClick", forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()
    
    //注册
    lazy var registerButton: UIButton = {
        let button = UIButton()
        // 添加事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()
    
    @objc private func buttonClick(){
    
        //执行闭包
        closure?()
    }
    
    //设置转圈动画
    func setAnima(){
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        
        anima.duration = 10
        
        anima.toValue = M_PI * 2
        
        anima.repeatCount = MAXFLOAT
        //当app推送到后台或切换控制器时，动画默认完成
        anima.removedOnCompletion = false
        //添加动画
        feedImageView.layer.addAnimation(anima, forKey: nil)
        
    }
    
    //提供给外界使用的方法
    func setupVisitorView(imgName: String?, title: String?){
        //如果有值就代表不是首页
        if let img = imgName, let tit = title{
        
            iconImageView.image = UIImage(named: img)
            messsageLabel.text = tit
            //隐藏圆圈
            feedImageView.hidden = true
        }else{
            
            setAnima()
        }
    
    }

}
